package com.shwread.android.qysw.activity.healthy;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.shwread.android.qysw.step.HealthyConfig;
import com.shwread.android.qysw.step.adapter.MyScoreAdapter;
import com.shwread.android.qysw.step.api.MyAsynctask;
import com.shwread.android.qysw.step.dto.HMMyScore;
import com.shwread.android.qysw.step.dto.HMMyScoreDto;
import com.shwread.android.qysw.step.model.UserHealthyModel;
import com.shwread.android.qysw.step.view.LoadMoreListView;
import com.shwread.android.qysw10000136.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import logic.constants.AppConfig;

/**
 * Created by Fantasy on 16/3/27.
 */
public class MyIntegralActivity extends Activity {
    @InjectView(R.id.back_btn)
    ImageView backBtn;
    @InjectView(R.id.back_text)
    TextView backText;
    @InjectView(R.id.title_text)
    TextView titleText;
    @InjectView(R.id.right_text)
    TextView rightText;
    @InjectView(R.id.right_img)
    ImageView rightImg;
    @InjectView(R.id.num)
    TextView num;
    @InjectView(R.id.integral_list_view)
    LoadMoreListView integralListView;
    private LinearLayoutManager linearLayoutManager;
    MyScoreAdapter myScoreAdapter;
    private boolean isHasMore = true;
    private int mPage = 1;
    private List<Object> mAllList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.healthy_my_integral_activity);
        ButterKnife.inject(this);
        setView();
        num.setText(UserHealthyModel.getInstance().getMyHmUserInfo().getScores() + "");
        linearLayoutManager = new LinearLayoutManager(this);
        integralListView.setLayoutManager(linearLayoutManager);
        mAllList = new ArrayList<>();
        myScoreAdapter = new MyScoreAdapter(MyIntegralActivity.this);

        myScoreAdapter.init_data_list(mAllList);
        integralListView.setLoadMoreListViewAdapter(myScoreAdapter);
        integralListView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                int lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
                int totalItemCount = linearLayoutManager.getItemCount();
                if (lastVisibleItem >= totalItemCount - 4 && dy >= 0 && isHasMore) {
                    refreshData(mPage);
                } else if (mAllList.size() < 4 && isHasMore) {
                    refreshData(mPage);
                }
            }
        });
        refreshData(1);
    }

    public void refreshData(int page) {

        String myUrl = "hzlqswserver/health/getScore";
        HashMap<String, String> hashMapUrl = new HashMap<>();
        hashMapUrl.put("url", myUrl);
        HashMap<String, String> params = new HashMap<>();
        params.put("userId", UserHealthyModel.getInstance().getMyHmUserInfo().getUserId()+"");
        params.put("enterpriseId", AppConfig.getEnterpriseId());
        params.put("page", page + "");
        params.put("pageSize", HealthyConfig.loadPageCount + "");
        MyAsynctask myAsynctask = new MyAsynctask(this, new MyAsynctask.RequestListener() {
            @Override
            public void requestSuccess(String result) {
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("returnObject");
                    HMMyScoreDto hmMyScoreDto = HMMyScoreDto.parserJson(res);
                    List<HMMyScore> hmMyScoreList = hmMyScoreDto.getData_list();

                    mAllList.addAll(hmMyScoreList);
                    myScoreAdapter.setTotal_data_list(mAllList);
                    myScoreAdapter.notifyDataSetChanged();
                    Log.i("darren", "size:----->" + hmMyScoreDto.getData_list().size());
                    if (hmMyScoreDto.getData_list().size() == HealthyConfig.loadPageCount) {
                        isHasMore = true;
                        mPage += 1;
                    } else {
                        isHasMore = false;
                    }
                    myScoreAdapter.setIsHasMore(isHasMore);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        myAsynctask.execute(hashMapUrl, params);
    }

    public void setView() {
        titleText.setText("我的积分");
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
